<?php
if(!Authenticate::isLogin() && !Authenticate::getAdminType()){
    throw new Exception('You not login');
}
$pdo = Db::getPDO();
$productSt = $pdo->prepare("SELECT * FROM Product WHERE id=:id LIMIT 1");
$packageSt = $pdo->prepare("SELECT * FROM Package WHERE id=:id LIMIT 1");
$invoiceSt = $pdo->prepare("SELECT * FROM Invoice WHERE productId=:productId ORDER BY createTime DESC LIMIT 1");
$productSt->execute(array('id'=> $_GET['id']));
$product = $productSt->fetch(PDO::FETCH_ASSOC);

if($product['accountId'] != Authenticate::getId() && !Authenticate::getAdminType()){
    throw new Exception('You not owner product');
}

$packageSt->execute(array('id'=> $product['packageId']));
$product['package'] = $packageSt->fetch(PDO::FETCH_ASSOC);

$invoiceSt->execute(array('productId'=> $_GET['id']));
$product['invoice'] = $invoiceSt->fetchAll(PDO::FETCH_ASSOC);

if($product['type'] == 'colo') $product = array_merge($product, ProductColo::instance()->getOne($_GET['id']));
if($product['type'] == 'vps') $product = array_merge($product, ProductVPS::instance()->getOne($_GET['id']));
if($product['type'] == 'hosting') $product = array_merge($product, ProductHosting::instance()->getOne($_GET['id']));
View::display('product', array('product'=> $product));